import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

// 1、页面跳转基本使用
//void main(){
//  runApp(new MaterialApp(
//    title: "",
//    home: new FirstScreen(),
//  ));
//}

class FirstScreen extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text("导航页面示例"),
        centerTitle: true,
      ),
      body: Center(
        child: RaisedButton(
            child: Text("查看商品详情页面"),
            onPressed: (){
              Navigator.push(
                  context,
                  new MaterialPageRoute(builder: (context) => new SecondScreen())
              );
            }
        ),
      ),
    );
  }

}

class SecondScreen extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text("导航页面示例"),
        centerTitle: true,
      ),
      body: new Center(
        child: RaisedButton(
            onPressed: (){
              Navigator.pop(context);
            },
            child: Text("返回页面"),
        ),
      ),
    );
  }

}

// 2、页面跳转发送数据

//void main(){
//  runApp(new MaterialApp(
//    title: "传递数据示例",
//    home: new ProductList(
//        products: new List.generate(20, (i) => new Product("商品$i", "这是一个商品详情$i")),
//    )
//  ));
//}

class Product{
  final String title;
  final String descnption;
  Product(this.title,this.descnption);
}

class ProductList extends StatelessWidget{

  final List<Product> products;
  ProductList({Key key,@required this.products}) : super(key : key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text("商品列表"),
        centerTitle: true,
      ),
      body: new ListView.builder(
          itemCount: products.length,
          itemBuilder: (context,index){
            return new ListTile(
              title: Text(products[index].title),
              onTap: (){
                Navigator.push(
                    context,
                    new MaterialPageRoute(
                        builder: (context) =>new ProductDetail(product: products[index],),
                    )
                );
              },
            );
          }
      ),
    );
  }

}

class ProductDetail extends StatelessWidget{
  final Product product;
  ProductDetail({Key key, @required this.product}) : super(key:key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text("${product.title}"),
      ),
      body: new Padding(
          padding: new EdgeInsets.all(16.0),
          child: Text("${product.descnption}"),
      ),
    );
  }
}

// 3、页面跳转返回数据

void main(){
  runApp(new MaterialApp(
      title: "页面跳转返回数据示例",
      home: new FirstPage()
  ));
}

class FirstPage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text("页面跳转返回数据示例"),
        centerTitle: true,
      ),
      body: new Center(
        child: new RouteButton(),
      ),
    );
  }

}

class RouteButton extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new RaisedButton(
        onPressed: (){
          _navigateToSecondPage(context);
        },
        child: Text("跳转第二个页面"),
    );
  }

}

_navigateToSecondPage(BuildContext context) async{

  final result = await Navigator.push(
      context,
      new MaterialPageRoute(
          builder: (context) => new SecondPage(),
      )
  );

  Scaffold.of(context).showSnackBar(new SnackBar(content: Text("$result")));
}

class SecondPage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text("选择一条数据"),
        centerTitle: true,
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Padding(
                padding: const EdgeInsets.all(8.0),
                child: RaisedButton(
                    onPressed: (){
                      Navigator.pop(context,"Hi Google");
                    },
                    child: Text("Hi Google"),
                ),
            ),
            new Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                onPressed: (){
                  Navigator.pop(context,"Hi Android");
                },
                child: Text("Hi Android"),
              ),
            ),
          ],
        ),
      ),
    );
  }

}

